﻿package com.thorqq.magictimer;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.thorqq.magictimer.SettingActivityTimer.TimeDefButtonListener;
import com.thorqq.magictimer.animation.ExpandAnimation;
import com.thorqq.magictimer.core.ChildView;
import com.thorqq.magictimer.core.ChildViewParent;
import com.thorqq.magictimer.core.TTimerDef;
import com.thorqq.magictimer.util.DateUtil;
import com.thorqq.magictimer.util.DisplayUtil;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

public class AdapterTimeDef extends BaseAdapter
{

    public final class ViewHolder
    {
        public boolean mInit = false;
        
        public TTimerDef timedef;
        public ChildView childView = null;
        public ChildViewParent parent;
        public int visibility = View.GONE;
        public LinearLayout layoutDown;
        public ExpandAnimation expandAni;

        public TextView time;
        public TextView name;
        public ImageView show;
        public LinearLayout layoutUp;
    }

    private LayoutInflater mInflater;
    private List<Map<String, Object>> mData;
    @SuppressWarnings("unused")
    private Context mContext;

    public AdapterTimeDef(Context context)
    {
        mInflater = LayoutInflater.from(context);
        mData = new ArrayList<Map<String, Object>>();
        mContext = context;
    }

    public AdapterTimeDef(Context context, List<Map<String, Object>> data)
    {
        mInflater = LayoutInflater.from(context);
        mData = data;
        mContext = context;
    }

    public void add(Map<String, Object> map)
    {
        mData.add(map);
    }

    public void removeItem(int position)
    {
        mData.remove(position);
    }
    
    public void updateItem(int position, String key, Object obj)
    {
        if (position < mData.size())
        {
            mData.get(position).put(key, obj);
        }
    }

    public int getCount()
    {
        return mData.size();
    }

    public Object getItem(int position)
    {
        if (position < mData.size())
        {
            return mData.get(position);
        }
        else
        {
            return null;
        }
    }

    public long getItemId(int position)
    {
        if (position < mData.size())
        {
            return position;
        }
        else
        {
            return -1;
        }
    }
    
    public View getView(final int position, View convertView, ViewGroup parent)
    {
        ViewHolder holder = null;
                
        if (convertView == null) 
        {            
            holder=new ViewHolder();  
            
            //布局
            convertView = mInflater.inflate(R.layout.timedef_item, null);
            holder.time = (TextView)convertView.findViewById(R.id.tvTime);
            holder.time.setTypeface(DisplayUtil.getNumTypeFace());
            holder.name = (TextView)convertView.findViewById(R.id.tvName);
            holder.show = (ImageView)convertView.findViewById(R.id.ivTimeDefShowMore);
            
            holder.layoutUp        = (LinearLayout)convertView.findViewById(R.id.LinearLayoutTimeSettingTime);
            holder.layoutDown        = (LinearLayout)convertView.findViewById(R.id.LayoutTimeDefSettingDown);
            
            convertView.setTag(holder);
        }
        else 
        {
            holder = (ViewHolder)convertView.getTag();
        }

        //传递过来的数据
        TimeDefButtonListener lster = (TimeDefButtonListener)mData.get(position).get("listener");

        //监听器
        holder.layoutUp.setOnClickListener(lster);

        holder.timedef = (TTimerDef)mData.get(position).get("timedef");
        holder.time.setText(DateUtil.formatTwoNumber(holder.timedef.getStartHour()) + ":" + 
                DateUtil.formatTwoNumber(holder.timedef.getStartMinute()) + " ");
        holder.name.setText(holder.timedef.getName());

        // 子布局
        setChildViewVisibility(holder, position);

        return convertView;    
    }
    
    private void setChildViewVisibility(ViewHolder holder, final int position)
    {
        // 子布局
        holder.visibility = (Integer) mData.get(position).get("visibility");
        holder.parent = (ChildViewParent) mData.get(position).get("parent");
        holder.childView = (ChildView) mData.get(position).get("childView");
        if(holder.childView == null)
        {
            return;
        }
        
        holder.childView.setTag(holder.timedef);
        holder.show.setVisibility(View.VISIBLE);

        if(holder.visibility != holder.layoutDown.getVisibility())
        {
            if(holder.visibility == View.VISIBLE)
            {
                if(!holder.mInit)
                {
                    // 子布局初始化
                    holder.childView.initLayout(holder.parent);
                    holder.childView.setParentScrollView((ScrollView)mData.get(position).get("scrollview"));
                    holder.layoutDown.addView(holder.childView.getLayoutView());
                    //必须要在 addView 之后才能 new ExpandAnimation
                    //每增加或者删除一个，就是调用new ViewHolder()
                    holder.expandAni = new ExpandAnimation(holder.layoutDown, ExpandAnimation.DEFAULT_DURATION);

                    holder.mInit = true;
                }
                else
                {
                    holder.childView.updateLayout();
                }
                
                holder.show.setImageResource(R.drawable.arrow_up);
            }
            else
            {
                holder.childView.onClose();
                holder.show.setImageResource(R.drawable.arrow_down);
            }
            
            holder.expandAni.reset();
            holder.expandAni.setVisibility(holder.visibility);
            holder.layoutDown.startAnimation(holder.expandAni);

        }
    }

}
